﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.ESS.BLL;
using Telerik.Web.UI;
using System.IO;
using KPIS.GERP.GFMS.DAL;

using KPIS.GERP.GFMS.APP.App_UI.Commons;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class TaskLists : System.Web.UI.Page
    {
        //static IList<M_FIM_ReceiveDtls> dtlsInfoList;
        Main main = new Main();
        B_MessageSend send = new B_MessageSend();
        B_MessageReply reply = new B_MessageReply();
        IBLL_FIM_MoneyRequests mr = new B_FIM_MoneyRequests();
        IBLL_FIM_MoneyRequestDtls mrd = new B_FIM_MoneyRequestDtls();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //======= test only =========
                //Session["EmpSeq"] = "1";
                //===========================

                cmdViewRequest_Click(null ,null);
            }
        }

        protected void LoadRadGridRequest()
        {
            string strEmpSeq = Session["EmpSeq"].ToString();

            RadGridRequest.DataSource = send.GetFull(strEmpSeq, null, null,null,null,null,null);
            RadGridRequest.DataBind();
        }

        protected void LoadRadGridReply()
        {
            string strEmpSeq = Session["EmpSeq"].ToString();

            RadGridReply.DataSource = reply.GetFull(strEmpSeq, null, null);
            RadGridReply.DataBind();
        }

        protected void LoadRadGridTaskList()
        {
            //string strUserSeq = Session["EmpSeq"].ToString();

            //RadGridRequest.DataSource = send.GetFull(strUserSeq, null, null);
            //RadGridRequest.DataBind();
        }

        protected string GetStatusPicture(string strMessageStatus)
        {
            string strUrl = "";

            switch (strMessageStatus)
            {
                case "0" :
                    strUrl = "../Images/Buttons/UnOpenMail.png";
                    break;
                case "1":
                    strUrl = "../Images/Buttons/OpenedMail.png";
                    break;
                case "2":
                    strUrl = "../Images/Buttons/ReplyedMail.png";
                    break;
            }

            return strUrl;
        }

        protected string GetStatusToolTip(string strMessageStatus)
        {
            string strText = "";

            switch (strMessageStatus)
            {
                case "0":
                    strText = "ยังไม่ได้เปิดอ่าน";
                    break;
                case "1":
                    strText = "เปิดอ่านแล้ว";
                    break;
                case "2":
                    strText = "ตอบกลับแล้ว";
                    break;
            }

            return strText;
        }

        public void ViewRequest()
        {
            LoadRadGridRequest();

            PanelRadGridRequest.Visible = true;
            PanelRadGridReply.Visible = false;
            PanelRadGridTaskList.Visible = false;
            PanelRequest.Visible = false;
            PanelReply.Visible = false;
        }

        public void ViewReply()
        {
            LoadRadGridReply();

            PanelRadGridRequest.Visible = false;
            PanelRadGridReply.Visible = true;
            PanelRadGridTaskList.Visible = false;
            PanelRequest.Visible = false;
            PanelReply.Visible = false;
        }

        public void ViewTaskList()
        {
            LoadRadGridTaskList();

            PanelRadGridRequest.Visible = false;
            PanelRadGridReply.Visible = false;
            PanelRadGridTaskList.Visible = true;
            PanelRequest.Visible = false;
            PanelReply.Visible = false;
        }

        //======================= PanelMenu ===============================

        protected void cmdViewRequest_Click(object sender, EventArgs e)
        {
            ViewRequest();
        }

        protected void cmdViewReply_Click(object sender, EventArgs e)
        {
            ViewReply();
        }

        protected void cmdViewTaskList_Click(object sender, EventArgs e)
        {
            ViewTaskList();
        }

        //======================= PanelRadGridRequest ===============================

        protected void RadGridRequest_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "RowClick" && e.Item is GridDataItem)
            {
                e.Item.Selected = true;
                string strSendSeq = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["send_seq"].Text);
                string strRefSeq = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["ref_seq"].Text);
                string strRefType = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["ref_type"].Text);
                string strFromUserSeq = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["from_emp_seq"].Text);
                string strToUserSeq = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["to_emp_seq"].Text);
                string strMessageStatus = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["message_status"].Text);
                string strFromSendSeq = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["from_send_seq"].Text);
                string strSendDate = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["send_date"].Text);
                string strRefNo = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["ref_no"].Text);
                string strSendText = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["send_text"].Text);
                string strFromUserName = main.WebString(RadGridRequest.Items[e.Item.ItemIndex]["from_user_name"].Text);

                switch (strRefType)
                {
                    case "fim_money_requests":
                        //Control ctrl = null;
                        PanelRadGridRequest.Visible = false;
                        PanelRadGridReply.Visible = false;
                        PanelRadGridTaskList.Visible = false;
                        PanelRequest.Visible = true;
                        PanelReply.Visible = false;
                        M_FIM_MoneyRequests info = mr.GetInfo(strRefSeq);

                        switch (info.event_seq.ToString())
                        {
                            case "16" :
                                BorrowRequest1.Visible = true;
                                Request1.Visible = false;

                                if (strMessageStatus != "2")
                                    BorrowRequest1.LoadData(strSendSeq, info, false);
                                else
                                    BorrowRequest1.LoadData(strSendSeq, info, true);
                                break;
                            case "12":
                                BorrowRequest1.Visible = false;
                                Request1.Visible = true;

                                if (strMessageStatus != "2")
                                    Request1.LoadData(strSendSeq, info, false);
                                else
                                    Request1.LoadData(strSendSeq, info, true);
                                break;
                        }
                    break;
                }
            }
        }

        //======================= PanelRadGridTaskList ===============================

        protected void RadGridReply_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "RowClick" && e.Item is GridDataItem)
            {
                e.Item.Selected = true;
                string strReplySeq = main.WebString(RadGridReply.Items[e.Item.ItemIndex]["reply_seq"].Text);

                PanelRadGridRequest.Visible = false;
                PanelRadGridReply.Visible = false;
                PanelRadGridTaskList.Visible = false;
                PanelRequest.Visible = false;
                PanelReply.Visible = true;
                Reply1.Visible = true;
                Reply1.LoadData(strReplySeq);
            }
        }

        //======================= PanelRadGridTaskList ===============================

        protected void RadGridTaskList_ItemCommand(object source, GridCommandEventArgs e)
        {
        }
    }
}
